<?php

namespace app\api\model\order;

use app\api\constant\OrderStatusConst;

trait OrderStatusTrait
{
    /**
     * 订单是否可以取消 （订单只有未付款状态才可以取消）
     * @return bool
     */
    public function canCancelHandle(): bool
    {
        return $this->order_status != OrderStatusConst::STATUS_CREATE;
    }

    /**
     * 是否可以付款
     * @return bool
     */
    public function canPayHandle(): bool
    {
        return $this->order_status == OrderStatusConst::STATUS_CREATE;
    }

    /**
     * 是否是待发货状态
     * @return bool
     */
    public function canShipHandle(): bool
    {
        return $this->order_status == OrderStatusConst::STATUS_PAY;
    }

    /**
     * 是否可以申请退款
     * @return int
     */
    public function canReFundHandle(): int
    {
        return $this->order_status == OrderStatusConst::STATUS_PAY;
    }

    /**
     * 是否可以同意退款
     * @return int
     */
    public function canAgreeRefundHandle()
    {
        return $this->order_status == OrderStatusConst::STATUS_PAY;
    }

    /**
     * 是否可以确认收货
     * @return int
     */
    public function canConfirmHandle(): int
    {
        return $this->order_status == OrderStatusConst::STATUS_SHIP;
    }

    /**
     * 可删除状态
     * @return bool
     */
    public function canDeleteHandle(): bool
    {
        return in_array($this->order_status, [
            OrderStatusConst::STATUS_CANCEL,
            OrderStatusConst::STATUS_ATO_CANCEL,
            OrderStatusConst::STATUS_ADMIN_CANCEL,
            OrderStatusConst::STATUS_REFUND_CONFIRM,
            OrderStatusConst::STATUS_CONFIRM,
            OrderStatusConst::STATUS_AUTO_CONFIRM,
        ]);
    }


    /**
     * 可评论状态
     * @return bool
     */
    public function canCommentHandle(): bool
    {
        return in_array($this->order_status, [
            OrderStatusConst::STATUS_CONFIRM,
            OrderStatusConst::STATUS_ADMIN_CANCEL
        ]);
    }

    public function canRebuyHandle(): bool
    {
        return in_array($this->order_status, [
            OrderStatusConst::STATUS_CONFIRM,
            OrderStatusConst::STATUS_AUTO_CONFIRM,
        ]);
    }

    /**
     * 可售后状态
     * @return bool
     */
    public function canAfterSaleHandle(): bool
    {
        return in_array($this->order_status, [
            OrderStatusConst::STATUS_CONFIRM,
            OrderStatusConst::STATUS_AUTO_CONFIRM,
        ]);
    }

    /**
     * @return array
     */
    public function getCanHandelOptions(): array
    {
        return [
            'cancel' => $this->canCancelHandle(),
            'delete' => $this->canDeleteHandle(),
            'pay' => $this->canPayHandle(),
            'comment' => $this->canCommentHandle(),
            'confirm' => $this->canConfirmHandle(),
            'refund' => $this->canReFundHandle(),
            'rebuy' => $this->canRebuyHandle(),
            'aftersale' => $this->canAfterSaleHandle()
        ];
    }
}